/*created by hp
*on 18-10-13 上午10:53
*/
package com.happts.UI;
import com.happts.Entity.RECORD;
import com.happts.Service.RECORDService;
import com.happts.Utils.CreateExcel;
import jxl.write.WriteException;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class SaveIn  {

    JTextField nameText, addressText, citeText, numberText, weightText, vegText, refineText;
    JLabel nameLabel, addressLabel, citeLabel, numberLabel, weightLabel, vegLabel, refineLabel;
    JPanel p1, p2, pp3;
    JButton jb_savein, jb_return;
    ImageIcon savein = new ImageIcon("src/com/happts/img/savein.png");
    ImageIcon ret = new ImageIcon("src/com/happts/img/return.png");

    SaveIn(){
        JFrame frame = UtilView.createFrame("用户登录");
        nameText = new JTextField(12);
        addressText = new JTextField(12);
        citeText = new JTextField(12);
        numberText = new JTextField(12);
        weightText = new JTextField();
        vegText = new JTextField();
        refineText = new JTextField();
        nameLabel = new JLabel("姓名");
        addressLabel = new JLabel("地址");
        citeLabel = new JLabel("代收点");
        numberLabel = new JLabel("单号");
        weightLabel = new JLabel("重量");
        vegLabel = new JLabel("菜籽油调率");
        refineLabel = new JLabel("精制油调率");

        jb_savein = new JButton(savein);
        jb_return = new JButton(ret);
        //设置布局
        frame.setLayout(null);
        //        frame.setLayout(new GridLayout(3, 2));
        frame.setSize(600, 350);
        frame.setLocation(750, 300);

        int x=30, y=50, w1=30, w2=200, h1= 30;
        nameLabel.setBounds(x, y, w1, h1);
        nameText.setBounds(x+w1+20, y, w2, h1);
        addressLabel.setBounds(x+w1+w2+40, y, w1, h1);
        addressText.setBounds(x+2*w1+w2+60, y, w2, h1);

        citeLabel.setBounds(x, y+h1+20,w1+10, h1);
        citeText.setBounds(x+w1+20, y+h1+20, w2, h1);
        numberLabel.setBounds(x+w1+w2+40, y+h1+20, w1, h1);
        numberText.setBounds(x+2*w1+w2+60,y+h1+20, w2, h1);

        w2 = 100;
        weightLabel.setBounds(x,y+2*h1+60, w1, h1);
        weightText.setBounds(x+w1+10, y+2*h1+60, w2, h1);
        vegLabel.setBounds(x+w1+w2+30, y+2*h1+60, 60, h1);
        vegText.setBounds(x+w1+w2+100,y+2*h1+60, w2, h1);
        refineLabel.setBounds(x+2*w1+w2+190, y+2*h1+60, 60, h1);
        refineText.setBounds(x+3*w1+w2+230, y+2*h1+60, w2, h1);

        jb_savein.setBounds(170, 250, 241, 37);
//        jb_login.setBounds(80, 150, 242, 37);
        jb_return.setBounds(500, 280, 75, 30);
        frame.add(nameLabel);
        frame.add(nameText);//第一块面板添加用户名和文本框

        frame.add(addressLabel);
        frame.add(addressText);//第二块面板添加密码和密码输入框
        frame.add(citeLabel);
        frame.add(citeText);
        frame.add(numberLabel);
        frame.add(numberText);
        frame.add(weightLabel);
        frame.add(weightText);
        frame.add(vegLabel);
        frame.add(vegText);
        frame.add(refineLabel);
        frame.add(refineText);

        frame.add(jb_savein); //第三块面板添加确认和取消
        frame.add(jb_return);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setTitle("存入");

        jb_savein.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                    String name = nameText.getText().trim();
                    String address = addressText.getText().trim();
                    String cite = citeText.getText().trim();
                    String number = numberText.getText().trim();
                    String weight = weightText.getText().trim();
                    String vegRate = vegText.getText().trim();
                    String refineRate = refineText.getText().trim();
                    if(name.equals("")||address.equals("")||cite.equals("")||number.equals("")||weight.equals("")||vegRate.equals("")||refineRate.equals("")){
                        JOptionPane.showMessageDialog(frame, "不允许为空", "存入失败", JOptionPane.WARNING_MESSAGE);
                    }
                    try{
                       int w = Integer.valueOf(weight);
                       double v = Double.valueOf(vegRate);
                       double r = Double.valueOf(refineRate);
                       if(v>1.0||v<0||r>1.0||r<0){
                           JOptionPane.showMessageDialog(frame, "调率不合理", "存入失败", JOptionPane.WARNING_MESSAGE);
                       }
                       else{
                            //输入成功，实例化存单，插入数据库
                           RECORD record = new RECORD(name,address,cite, number, w, v,r);
                           RECORDService rs = new RECORDService();
                           if (rs.add(record)){
                                //生成存单
                               CreateExcel ce = new CreateExcel();
                               try {
                                   ce.saveIn(record);
                               }
                               catch (IOException e1) {
                                   e1.printStackTrace();
                                   JOptionPane.showMessageDialog(frame, "存单生成失败", "出错", JOptionPane.WARNING_MESSAGE);
                               }
                               catch (WriteException e1) {
                                   e1.printStackTrace();
                                   JOptionPane.showMessageDialog(frame, "存单生成失败", "出错", JOptionPane.WARNING_MESSAGE);
                               }
                               JOptionPane.showMessageDialog(frame, "添加成功", "成功", JOptionPane.YES_NO_CANCEL_OPTION);
                               frame.dispose();

                               //界面跳转
                               new Menu();
                           }
                           else{
                               JOptionPane.showMessageDialog(frame, "添加失败", "存入失败", JOptionPane.WARNING_MESSAGE);
                           }
                       }
                    }catch (NumberFormatException e1){
                        JOptionPane.showMessageDialog(frame, "不是数值类型", "存入失败", JOptionPane.WARNING_MESSAGE);
                    }
            }
        });
        jb_return.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Menu();
            }
        });
    }

    public static void main(String[] args){
        new SaveIn();
    }
}
